package android.kryten.service.impl;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.kryten.model.News;
import android.kryten.model.Objective;
import android.kryten.service.NewsService;
import android.util.Log;

public class NewsServiceImpl implements NewsService {

	private static final String TAG = "NEWS_SERVICE";
	private final String URL_GET_ALL = "opensocial/ui/news/ajaxlist/";
	private final String URL_GET_MY = "";
	public HttpClient client;
	
	public NewsServiceImpl(){
		super();
		this.client = new DefaultHttpClient();
	}
	
		
	public List<News> getAllNewsTEST() {
		 
		List<News> result = new ArrayList<News>(); 
		
//		result.add(new News("Karel Radocha","First news"));
//        result.add(new News("Luk� Rejha","Second news"));
        
        //simulate network latency
        try {
            Random rnd = new Random();
            Thread.sleep(2000+rnd.nextInt(3000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return result;
		
	}

	
	public List<News> getAllNews() {
		
		List<News> result = new ArrayList<News>();
        try {
        	
        	HttpPost req = new HttpPost("http://10.0.2.2:8080/" + URL_GET_ALL);
          	            
            HttpResponse resp = client.execute(req);
            if (resp.getStatusLine().getStatusCode() != 200) {
                Log.e(TAG, "Bad server response code:"+resp.getStatusLine().getStatusCode());
                //there we can return null, or throw an exception, because nothing better cannot be done
                return null;
            }
           
            StringBuilder sb = new StringBuilder();
            
            BufferedReader br = new BufferedReader(new InputStreamReader(resp.getEntity().getContent(), "UTF-8"));
            String line = "";
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            
            String json = sb.toString();
            
            JSONObject container = new JSONObject(json);
            	            
            JSONArray newses = container.getJSONArray("items");
            for (int i = 0; i < newses.length(); i++) {
                JSONObject n = newses.getJSONObject(i);
                
                News news = new News();
                
 //               news.setAuthor(n.getString("author"));
                news.setNews(n.getString("news"));
                
                result.add(news);
            }	                        
            
        } catch (Exception e) {
            Log.e(TAG, "Unable to retrieve obejctives", e);
        }
        	        
        return result;
	}
	
	
}
